package org.dawfinn.droiditarian.listener;

import org.dawfinn.droiditarian.R;
import org.dawfinn.droiditarian.data.Deed;
import org.dawfinn.droiditarian.data.DeedsDBAdapter;
import org.dawfinn.droiditarian.deedcompleteupload.DeedCompleteUpload;
import org.dawfinn.droiditarian.deedcompleteupload.InternetDeedCompleteUpload;
import org.dawfinn.droiditarian.deedlocationupload.DeedLocationUpload;
import org.dawfinn.droiditarian.deedlocationupload.InternetDeedLocationUpload;

import android.content.Context;
import android.graphics.Bitmap;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.widget.Toast;

public class CompletedAdapter {

	protected static final String COMPLETE_UPLOAD_PATH = "http://droiditarian.appspot.com/" + "complete";
	private static final String LOCATION_UPLOAD_PATH = "http://droiditarian.appspot.com/" + "location";

	private final Deed deed;
	private final LocationManager locationManager;
	private final DeedsDBAdapter db;
	private final Context context;

	public CompletedAdapter(Deed deed, Context context) {
		this.deed = deed;
		this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		this.db = new DeedsDBAdapter(context);
		this.context = context;
	}

	public void saveDeedState() {
		boolean isNowCompleted = !deed.isCompleted();
		deed.setCompleted(isNowCompleted);
		db.open();
		db.updateDeed(deed);
		db.close();
		deed.notifyOnDeedChangeListener();
	}

	public void uploadCompleteToServer() {
		new CompletedAsyncTask().execute();
	}

	class CompletedAsyncTask extends AsyncTask<Bitmap, Boolean, Boolean> {

		@Override
		protected Boolean doInBackground(Bitmap... images) {
			DeedCompleteUpload completeUploader = new InternetDeedCompleteUpload(COMPLETE_UPLOAD_PATH);
			completeUploader.sendDeedComplete(deed, deed.isCompleted());
			return true;
		}

	}

	public void uploadLocationToServer(Bitmap bitmap) {
		new LocationAsyncTask().execute(bitmap);
	}

	class LocationAsyncTask extends AsyncTask<Bitmap, Boolean, Boolean> {		
		@Override
		public Boolean doInBackground(Bitmap... images) {
			DeedLocationUpload locUploader = new InternetDeedLocationUpload(LOCATION_UPLOAD_PATH);
			Location currentLocation = getCurrentLocation();
			if (currentLocation != null) {
				locUploader.sendLocation(currentLocation, deed, images[0]);
				return true;
			}
			return false;

		}

		@Override
		protected void onPostExecute(Boolean result) {
			if (result)
				Toast.makeText(context, R.string.location_uploaded, Toast.LENGTH_LONG).show();
			else
				Toast.makeText(context, R.string.location_not_uploaded, Toast.LENGTH_LONG).show();
		}
	}

	private Location getCurrentLocation() {
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_COARSE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);

		Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
		return location;
	}
}
